﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using System.Threading;
using System.IO;
using ICSharpCode.SharpZipLib.Zip.Compression;
using ICSharpCode.SharpZipLib.Zip.Compression.Streams;
using ICSharpCode.SharpZipLib.GZip; 

namespace PerformanceTests
{
    partial class Program
    {

        static void CheckCompressedText(byte[] compressed, string[] uncompressedStrings, Encoding encoding)
        {
            string uncompressed = ""; 
            foreach (string cs in uncompressedStrings)
            {
                uncompressed += cs;
            }
            

            bool gzip = (compressed[0] == 31) && (compressed[1] == 139) && (compressed[2] == 8);

            int bytesRead = -1;
            byte[] uncompressed2 = null;
            if (gzip)
            {
                MemoryStream stream = new MemoryStream(compressed);
                GZipInputStream compression = new GZipInputStream(stream);
                uncompressed2 = new byte[uncompressed.Length + 1];
                bytesRead = compression.Read(uncompressed2, 0, uncompressed2.Length);
            }
            else
            {
                bool zlib = (compressed[0] & 0x0F) == 8;
                zlib = zlib && (((compressed[0] * 256 + compressed[1]) % 31) == 0);

                MemoryStream stream = new MemoryStream(compressed);
                Inflater inflater = new Inflater(!zlib);
                var compression = new InflaterInputStream(stream, inflater);
                uncompressed2 = new byte[uncompressed.Length*4];
                bytesRead = compression.Read(uncompressed2, 0, uncompressed2.Length);
            }

            string uncompressedString = encoding.GetString(uncompressed2, 0, bytesRead);

            if (uncompressed.Length != uncompressedString.Length)
            {
                Console.WriteLine("Check Failed, Data length should be " + uncompressed.Length.ToString() + " but is " + uncompressedString.Length.ToString());
            }
            else
            {
                bool error = false;
                for (int index = 0; index < uncompressed.Length; ++index)
                {
                    if (!error)
                    {
                        if (uncompressed[index] != uncompressedString[index])
                        {
                            error = true;

                            Console.WriteLine("Check Failed, Data differ on Index "
                            + index.ToString() +
                            " byte shoud be " + uncompressed[index].ToString() +
                            " but is " + uncompressedString[index].ToString());
                        }
                    }
                }
                if (!error)
                    Console.WriteLine("Check Completed without Errors");

            }
        
        }


        static void CheckCompressedData(byte[] compressed, byte[] uncompressed)
        {
            bool gzip = (compressed[0] == 31) && (compressed[1] == 139) && (compressed[2] == 8);

            int bytesRead = -1;
            byte[] uncompressed2 = null;
            if (gzip)
            {
                MemoryStream stream = new MemoryStream(compressed);
                GZipInputStream compression = new GZipInputStream(stream);
                uncompressed2 = new byte[uncompressed.Length + 1];
                bytesRead = compression.Read(uncompressed2, 0, uncompressed2.Length);
            }
            else
            {
                bool zlib = (compressed[0] & 0x0F) == 8;
                zlib = zlib && (((compressed[0] * 256 + compressed[1]) % 31) == 0);

                MemoryStream stream = new MemoryStream(compressed);
                Inflater inflater = new Inflater(!zlib);
                var compression = new InflaterInputStream(stream, inflater);
                uncompressed2 = new byte[uncompressed.Length + 1];
                bytesRead = compression.Read(uncompressed2, 0, uncompressed2.Length);
            }

            if (uncompressed.Length != bytesRead)
            {
                Console.WriteLine("Check Failed, Data length should be " + uncompressed.Length.ToString() + " but is " + bytesRead.ToString());
            }
            else
            {
                bool error = false;
                for (int index = 0; index < uncompressed.Length; ++index)
                {
                    if (!error)
                    {
                        if (uncompressed[index] != uncompressed2[index])
                        {
                            error = true;

                            Console.WriteLine("Check Failed, Data differ on Index "
                            + index.ToString() +
                            " byte shoud be " + uncompressed[index].ToString() +
                            " but is " + uncompressed2[index].ToString());
                        }
                    }
                }
                if (!error)
                    Console.WriteLine("Check Completed without Errors");

            }
        }

    }
}
